package com.example.wechat.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.bumptech.glide.Glide;
import com.example.wechat.R;
import com.example.wechat.model.User;
import com.example.wechat.util.UserManager;

public class ProfileActivity extends AppCompatActivity {
    private ImageView avatarView;
    private TextView nicknameView;
    private TextView wechatIdView;
    private TextView genderView;
    private TextView regionView;
    private TextView signatureView;
    private User currentUser;
    private ActivityResultLauncher<Intent> imagePickerLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("个人信息");

        initViews();
        setupImagePicker();
        loadUserInfo();
    }

    private void initViews() {
        avatarView = findViewById(R.id.avatar);
        nicknameView = findViewById(R.id.nickname);
        wechatIdView = findViewById(R.id.wechat_id);
        genderView = findViewById(R.id.gender);
        regionView = findViewById(R.id.region);
        signatureView = findViewById(R.id.signature);

        // 设置点击事件
        findViewById(R.id.avatar_item).setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            imagePickerLauncher.launch(intent);
        });

        findViewById(R.id.nickname_item).setOnClickListener(v -> {
            // TODO: 跳转到修改昵称页面
        });

        findViewById(R.id.wechat_id_item).setOnClickListener(v -> {
            // TODO: 跳转到修改微信号页面
        });

        findViewById(R.id.gender_item).setOnClickListener(v -> {
            // TODO: 跳转到修改性别页面
        });

        findViewById(R.id.region_item).setOnClickListener(v -> {
            // TODO: 跳转到修改地区页面
        });

        findViewById(R.id.signature_item).setOnClickListener(v -> {
            // TODO: 跳转到修改个性签名页面
        });
    }

    private void setupImagePicker() {
        imagePickerLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                    Uri selectedImage = result.getData().getData();
                    // 更新头像
                    updateAvatar(selectedImage);
                }
            }
        );
    }

    private void loadUserInfo() {
        currentUser = UserManager.getInstance().getCurrentUser();
        if (currentUser != null) {
            nicknameView.setText(currentUser.getNickname());
            wechatIdView.setText(currentUser.getWechatId());
            genderView.setText(currentUser.getGender());
            regionView.setText(currentUser.getRegion());
            signatureView.setText(currentUser.getSignature());

            // 加载头像
            Glide.with(this)
                    .load(currentUser.getAvatarUrl())
                    .placeholder(R.drawable.default_avatar)
                    .error(R.drawable.default_avatar)
                    .circleCrop()
                    .into(avatarView);
        }
    }

    private void updateAvatar(Uri imageUri) {
        // TODO: 上传头像到服务器
        // 临时更新本地显示
        Glide.with(this)
                .load(imageUri)
                .circleCrop()
                .into(avatarView);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 